package ui;

import base.Food;
import com.sun.lwuit.Button;
import com.sun.lwuit.Component;
import com.sun.lwuit.Container;
import com.sun.lwuit.Dialog;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BoxLayout;
import db.FoodManager;
import java.util.Vector;

/**
 *
 * @author Shayan
 */
public class FoodSelector extends Container implements ActionListener {

    private Vector foods;
    private FoodSelectionListener listener;

    public void setFoodSelectionListener(FoodSelectionListener listener) {
        this.listener = listener;
    }

    public FoodSelector(String categoryId) {
        try {

            foods = FoodManager.getAllByCategory(categoryId);
            setLayout(new BoxLayout(BoxLayout.Y_AXIS));
            for (int i = 0; i < foods.size(); i++) {
                final Food c = (Food) foods.elementAt(i);
                Button btn = new Button(c.getName());
                btn.addActionListener(this);
                btn.setUIID("FoodButton");
                addComponent(btn);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            Dialog.show("Error", ex.toString(), "OK", "");
        }
    }

    public void actionPerformed(ActionEvent e) {
        Button btn = (Button) e.getSource();
        for (int i = 0; i < getComponentCount(); i++) {
            Component c = getComponentAt(i);
            if (c != btn) {
                // do something
            }
        }
        if (btn != null && listener != null) {
            listener.foodSelected(btn.getText());
        }
    }
}

